package com.robot.orders.job;

import com.alibaba.fastjson.JSON;
import com.robot.orders.client.MemberClient;
import com.robot.orders.client.MerchantClient;
import com.robot.orders.pojo.Orders;
import com.robot.orders.pojo.client.Member;
import com.robot.orders.pojo.client.Store;
import com.robot.orders.service.OrdersService;
import com.robot.orders.service.WeChatPayService;
import entity.weixin.template.PaySuccess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author wuyunbin
 */
@Component
@RabbitListener(queues = "query")
public class OrdersJob {
    private String success ="SUCCESS";
    private String refund="REFUND";
    private String notPay="NOTPAY";

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private WeChatPayService weChatPayService;

    @Autowired
    private MerchantClient merchantClient;

    @Autowired
    private MemberClient memberClient;

    @RabbitHandler
    public void execQuery(String ordersId) {
        Map<String,String> response=  weChatPayService.query(ordersId);
        logger.info("=========query response=============");
        logger.info("query jobs ordersId="+ordersId);
        logger.info(response.toString());
        logger.info("=========query response=============");
        if(success.equals(response.get("trade_state"))){
            //支付成功
            Orders orders=ordersService.findById(ordersId);
            if(orders!=null){
                orders.setTransactionNo(response.get("transaction_id"));
                orders.setStatus("1");
                String remake=orders.getRemark();
                if(!"".equals(remake)){
                    ordersService.updateIsPlus(ordersId);
                }
                Store store=JSON.parseObject(merchantClient.findEnableStoreById(orders.getStoreId()),Store.class) ;
                orders.setAgentId(store.getAgentId());
                orders.setCompanyId(store.getCompanyId());
                orders.setStoreName(store.getStoreName());
                orders.setArea(store.getArea());
                Member member=JSON.parseObject(memberClient.thirdFindEnableMemberById(orders.getMemberId()),Member.class) ;
                orders.setNickname(member.getNickname());
                orders.setPhone(member.getPhone());
                logger.info(orders.toString());
                ordersService.update(orders);
                //准备发送通知
                PaySuccess paySuccess=new PaySuccess();
                paySuccess.setRecipient(response.get("openid"));
                paySuccess.setTemplateId("nNwPM0kmNw8jxLp_P5gKlvq7JhDIdYrVkpNLGAprG8w");
                logger.info("nickname="+orders.getNickname());
                logger.info("nickname2="+member.getNickname());
                String first="会员"+member.getNickname()+"消费成功";
                paySuccess.setFirst(first);
                paySuccess.setKeyword1(orders.getMemberId());
                paySuccess.setKeyword2(orders.getNickname());
                paySuccess.setKeyword3(orders.getPayAmount()+"");
                paySuccess.setKeyword4(orders.getPrice()+"");
                paySuccess.setKeyword5("");
                paySuccess.setRemark("示例备注");

                logger.info(paySuccess.getClass()+"-notify-"+ JSON.toJSONString(paySuccess));
                rabbitTemplate.convertAndSend("notify", "PaySuccess-notify-"+ JSON.toJSONString(paySuccess) );
            }
        }
    }
}
